package com.ksd.api.controller;

import com.ksd.api.utils.HttpServletUtil;
import com.ksd.system.domain.*;
import com.ksd.system.service.*;
import com.ksd.api.beans.GetLiveResponse;
import com.ksd.common.config.PlayControlConfig;
import com.ksd.common.core.controller.BaseController;
import com.ksd.common.core.domain.AjaxResult;
import com.ksd.quartz.util.TaskConstans;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
public class GetLiveList extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(GetLiveList.class);

    @Autowired
    private ISysChannelTemplateService sysChannelTemplateService;
    @Autowired
    private ISysChannelService sysChannelService;
    @Autowired
    private ISysBoardService sysBoardService;

    @RequestMapping(value = "/api/user/live/getLiveList", method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult getLiveList(HttpServletRequest httpServletRequest) {
        logger.info("===================获取直播频道===============");

        String pageSize = httpServletRequest.getParameter("pageSize");
        String pageNum = httpServletRequest.getParameter("pageNum");
        HttpServletUtil.getRequestParam(httpServletRequest);

        SysChannel sysChannelOne1 = new SysChannel();
        List<SysChannel> sysChannelOnes1 = sysChannelService.selectSysChannelList(sysChannelOne1, SysChannel.table_one);

        SysChannel sysChannelOne2 = new SysChannel();
        sysChannelOne2.setStatus2(TaskConstans.status_selected);
        List<SysChannel> sysChannelOnes2 = sysChannelService.selectSysChannelList(sysChannelOne2, SysChannel.table_twice);

        SysChannel sysChannelTwice1 = new SysChannel();
        sysChannelTwice1.setStatus1(TaskConstans.status_selected);
        List<SysChannel> sysChannelTwices1 = sysChannelService.selectSysChannelList(sysChannelTwice1, SysChannel.table_twice);

        SysChannel sysChannelTwice2 = new SysChannel();
        sysChannelTwice2.setStatus2(TaskConstans.status_selected);
        List<SysChannel> sysChannelTwices2 = sysChannelService.selectSysChannelList(sysChannelTwice2, SysChannel.table_twice);

        List<GetLiveResponse> data = new ArrayList<>();
//        System.out.println("直播板卡1频道个数:"+sysChannelOnes1.size()+sysChannelOnes2.size());
//        System.out.println("直播板卡2频道个数:"+sysChannelTwices1.size()+sysChannelTwices2.size());

        SysChannelTemplate sysChannelTemplate = new SysChannelTemplate();
        sysChannelTemplate.setStatus(TaskConstans.status_selected);
        List<SysChannelTemplate> sysChannelTemplates = sysChannelTemplateService.selectSysChannelTemplateListByChannelId(sysChannelTemplate);
//        System.out.println("查询到的总个数:"+sysChannelTemplates.size());
        for (SysChannelTemplate channelTemplate : sysChannelTemplates) {
            Long boardId = channelTemplate.getBoardId();
            if (TaskConstans.boardValueCustom.equals(String.valueOf(boardId))){
                //自定义频道
                Long channelId = channelTemplate.getChannelId();
                String formattedNumber = String.format("%02d", channelId);
                GetLiveResponse getLiveResponse = new GetLiveResponse();
                getLiveResponse.setId(Integer.valueOf(channelTemplate.getId().toString()));
                getLiveResponse.setChannelId(formattedNumber);
                getLiveResponse.setName(channelTemplate.getName());
                getLiveResponse.setLogo(channelTemplate.getLogo());
                String urlType = channelTemplate.getUrlType();
                if (urlType.equals("1")){
                    //U盘
                    SysBoard sysBoard = sysBoardService.selectSysBoardById(SysBoard.serviceFileId);
                    getLiveResponse.setUrl(sysBoard.getUrl()+channelTemplate.getSourceUrl());
                }else if(urlType.equals("2")){
                    //网络
                    getLiveResponse.setUrl(channelTemplate.getSourceUrl());
                }
                getLiveResponse.setStatus(channelTemplate.getStatus());
                data.add(getLiveResponse);
            }else{
                Long channelId = channelTemplate.getChannelId();
                String formattedNumber = String.format("%02d", channelId);
                GetLiveResponse getLiveResponse = new GetLiveResponse();
                getLiveResponse.setId(Integer.valueOf(channelTemplate.getId().toString()));
                getLiveResponse.setChannelId(formattedNumber);
                getLiveResponse.setName(channelTemplate.getName());
                getLiveResponse.setLogo(channelTemplate.getLogo());
                getLiveResponse.setUrl(PlayControlConfig.getChannelUrlStart() +channelTemplate.getIp()+":"+channelTemplate.getPort());
                getLiveResponse.setStatus(channelTemplate.getStatus());
                data.add(getLiveResponse);
            }
        }

        return AjaxResult.success(data);
    }


}